package com.gl.glcourses.util;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;
import com.gl.glcourses.vos.Usuario;

public class HiloMail extends Thread {
	
	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private VelocityEngine velocityEngine;
	@Autowired
	private String appUrl;
	private Usuario user;
	private String template;
	
	public HiloMail(Usuario user, String template){
		this.user = user;
		this.template = template;
	}
	
	public void run(){
		
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
			public void prepare(MimeMessage mimeMessage) {
				try {
				
					MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
		            message.setTo("CORREO ELECTRONICO DEL USUARIO DESTINATARIO");
		            //message.setTo("octavio_ar21@hotmail.com");
		            message.setFrom("robot@globallogic.com");
		            message.setSubject("Biblioteca");
		            Map model = new HashMap();
		            model.put("user", user);
		            String text = VelocityEngineUtils.mergeTemplateIntoString(
		               velocityEngine, "mail/"+template, model);
		            message.setText(text, true); 	
					
				} catch (Exception e) {
					e.printStackTrace();
				}  
	         }
	      };
	      this.mailSender.send(preparator); 	
	}

}
